/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package com.google.code.twiddling.core.util;

import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * @author <a href="mailto:jeff.yuchang@gmail.com">Jeff Yu</a>
 *
 */
public class StringUtil {
	
	
	public static String[] getStringBySpaceDelimiter(String cmd) {
		List<String> result = new LinkedList<String>();
		StringTokenizer st = new StringTokenizer(cmd, " ");
		while (st.hasMoreTokens()) {
			String token = st.nextToken();
			if (token != null && token.trim().length() > 0) {
				result.add(token.trim());
			}
		}
		String[] strResult = new String[result.size()];
		return result.toArray(strResult);
	}
	
	/**
	 * Return a String from a String array that contact by specified delimeter.
	 * 
	 * @param string
	 * @param delimeter
	 * @return
	 */
	public static String getArrayString(String[] string, String delimeter) {
		StringBuffer result = new StringBuffer();
		if (string == null || string.length < 1) {
			return result.toString();
		}
		for (int i =0; i < string.length; i++) 
		{
			result.append(string[i]);
			if (i < string.length -1) {
				result.append(delimeter);
			}
		}
		return result.toString();
	}
	
	
    public static boolean isEmpty(String str) {
        if (str != null && str.trim().length() > 0) {
            return false;
        }
        return true;
    }
	
	/**
	 * Get PackageName
	 * @param className
	 * @return
	 */
    static String getPackageName(String className) {
        int pos = className.lastIndexOf('.');
        if (pos != -1) {
            return className.substring(0, pos);
        } else {
            return "";
        }
    }
    
    public static String getPackageName(Class<?> clazz) {
        String className = clazz.getName();
        if (className.startsWith("[L")) {
            className = className.substring(2);
        }
        return getPackageName(className);
    }
	
}
